# 生成老师授课基本信息 老师详情页 统计学生数，课程详情
from api.handler.ce.AbstractBatchCE import AbstractBatchCE
from api.model.BatchOutputPipe import BatchOutputPipe
from api.model.FdInputPipe import FdInputPipe
from munch import Munch
import datetime
# from config.annotation import Value


def get_xuenian():
    """获取学年"""
    now_year = int(datetime.datetime.now().year)
    return '{}-{}'.format(now_year-1, now_year)

class TeachCourseHandler(AbstractBatchCE):
    """生成老师授课基本信息"""
    def do_compute(self, output_stream: BatchOutputPipe, source_fds: FdInputPipe, params: dict):
        teacher_fd = source_fds.get_fd('VDJ_NEWJW_JSKB')       # 教务-教师课表

        teacher_sql = """select a.id, a.RKJSGH as code, a.JSXS as time, a.skbjrs as studentnum, KCH
FROM vdj_jw_jskb a
WHERE a.xn = '{}'
        """.format(get_xuenian())
        teacher_results = teacher_fd.query(teacher_sql)

        kc_fd = source_fds.get_fd('VDJ_JW_KCSJLB')
        kc_sql = "SELECT distinct KCH, KCMC FROM VDJ_JW_KCSJLB where KCZT = '开设'"
        kcs = kc_fd.query(kc_sql)
        self.task_logger.log('老师人数：{}\n 课程数{}'.format(len(teacher_results), len(kcs)))

        for result in teacher_results:
            data = Munch()
            data.id = result.get('id')
            data.code = result.get('code', '')
            data.studentnum = result.get('studentnum', 0)
            data.time = result.get('time', 0)
            for kc in kcs:
                if result.KCH == kc.KCH:
                    data.coursename = kc.KCMC
                    break
            output_stream.output_for_upsert(data)


